package com.qianxx.base.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.qianxx.base.utils.ScreenUtils;
import com.qianxx.draw.R;

public class Indicator extends View {

	public Indicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initX(context);
		initPaint();
	}

	private void initX(Context context) {
		cx = ScreenUtils.getScreenWitdh(context) / 2 - 4.5f * radius;
	}

	private Paint mPaint;
	private float cx = 50;
	private float cy = 20;
	private float radius = 9;
	private float radiusTop = 9;
	private float offsetX;
	private Paint mPaintSroke;
	private int count = 4;
	private Context context;
	private int c; // 偏差
	private float density;

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		if (density != 0) {
			// 避免因onMeasure的多次调用而重复计算
			return;
		}
		DisplayMetrics dm = getResources().getDisplayMetrics();
		density = dm.density;
		radius = 5 * density;
		radiusTop = 5 * density;
	}

	private void initPaint() {
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		
		mPaintSroke = new Paint();
		mPaintSroke.setAntiAlias(true);
		mPaintSroke.setColor(getResources().getColor(R.color.indicator_around));
		mPaintSroke.setStrokeWidth(1);
		mPaintSroke.setStyle(Paint.Style.STROKE);
	}

	public void setCount(int count) {
		this.count = count;
		cx = ScreenUtils.getScreenWitdh(context) / 2 - c - (count - 1) * 3
				* radius * 0.5f;
		invalidate();
	}

	public void setC(int c) {
		this.c = c;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		cx = ScreenUtils.getScreenWitdh(context) / 2 - c - (count - 1) * 3
				* radius * 0.5f;
		mPaint.setColor(getResources().getColor(R.color.indicator_in));
		for (int i = 0; i < count; i++) {
			canvas.drawCircle(cx + radius * 3 * i, cy, radius, mPaint);
			canvas.drawCircle(cx + radius * 3 * i, cy, radius, mPaintSroke);
		}

		mPaint.setColor(getResources().getColor(R.color.indicator_color));
		canvas.drawCircle(cx + offsetX, cy, radiusTop, mPaint);
	}

	public void move(int pos, float x) {
		offsetX = (x + pos) * radius * 3;
		invalidate();
	}

}
